<template>
  <div>
    <div class="top-area">
      <el-input
        v-model="dishName"
        placeholder="请输入菜品名称"
        clearable
        @keyup.native.enter="init"
        ><i
          slot="prefix"
          class="el-input__icon el-icon-search"
          @click="init"
        ></i
      ></el-input>
      <div class="button-group">
        <el-link type="danger" @click="batchDelete">批量删除</el-link>
        <el-divider direction="vertical"></el-divider>
        <el-link type="primary" @click="batchEdit('start')">批量启售</el-link>
        <el-divider direction="vertical"></el-divider>
        <el-link type="danger" @click="batchEdit('stop')">批量停售</el-link>
        <el-button
          @click="
            $router.push({
              name: 'editDish',
              query: { isEdit: '0' },
            })
          "
          icon="el-icon-plus"
          >新建菜品</el-button
        >
      </div>
    </div>
    <div class="table-container">
      <el-table
        ref="multipleTable"
        border
        :data="tableData"
        style="width: 100%"
        max-height="527"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="name" label="菜品名称"> </el-table-column>
        <el-table-column prop="image" label="图片">
          <template slot-scope="scope">
            <img
              :src="`${env}/common/download?name=${scope.row.image}`"
              alt=""
              style="width: 50px; height: 50px"
            />
          </template>
        </el-table-column>
        <el-table-column prop="categoryName" label="菜品分类">
        </el-table-column>
        <el-table-column prop="price" label="售价">
          <template slot-scope="scope">
            ￥{{ scope.row.price / 100 }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="售卖状态">
          <template slot-scope="scope">
            {{ scope.row.status ? "启售" : "停售" }}
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="最后操作时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link
              type="primary"
              @click="
                $router.push({
                  name: 'editDish',
                  query: { isEdit: '1', id: scope.row.id },
                })
              "
              >修改</el-link
            >
            <el-divider direction="vertical"></el-divider>
            <el-popconfirm
              title="确认调整该账号的状态?"
              @onConfirm="handleChange(scope.row)"
              ><el-link type="primary" slot="reference">{{
                scope.row.status ? "停售" : "启售"
              }}</el-link>
            </el-popconfirm>
            <el-divider direction="vertical"></el-divider>
            <el-popconfirm
              title="确认删除该菜品?"
              @onConfirm="handleDelete(scope.row.id)"
              ><el-link type="danger" slot="reference">删除</el-link>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="el-pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.page"
          :page-sizes="pageSizes"
          :page-size="pageInfo.pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getDishes,
  startDishStatus,
  stopDishStatus,
  batchDeleteDishes,
} from "@/api/dishManage";
export default {
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      dishName: "",
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      pageSizes: [10, 20, 30, 40],
      tableData: [],

      //用户勾选
      ids: "",
      multipleSelection: [],
    };
  },
  created() {
    this.init();
  },
  computed: {
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize;
      return this.tableData.slice(start, start + this.pageSize);
    },
  },
  methods: {
    init() {
      //   console.log("合并之后", { ...this.pageInfo, name: this.dishName });
      getDishes({ ...this.pageInfo, name: this.dishName }).then((res) => {
        const { records, total } = res.data;
        this.tableData = records;
        console.log("tableData", this.tableData);
        this.total = total;
      });
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    handleSizeChange(val) {
      this.pageInfo.pageSize = val; // 更新每页显示的数据条数
      this.pageInfo.page = 1; // 切换每页数据时重置到第一页
      this.init();
    },
    handleChangeStatus(row) {
      changeEmployee({ id: row.id, status: +!row.status }).then((res) => {
        this.$message.success("修改成功");
        this.init();
      });
    },
    //行内启售、停售
    async handleChange(row) {
      console.log("row", row);
      if (row.status == 0) {
        //停售
        const res = await startDishStatus(row.id);
        if (res.code === 1) {
          this.$message.success(res.data);
          this.init();
        } else {
          this.$message.error(res.msg);
        }
      } else if (row.status == 1) {
        //启售
        const res = await stopDishStatus(row.id);
        if (res.code === 1) {
          this.$message.success(res.data);
          this.init();
        } else {
          this.$message.error(res.msg);
        }
      }
    },

    //行内删除
    async handleDelete(id) {
      console.log("id", id);
      const res = await batchDeleteDishes(id);
      if (res.code === 1) {
        this.$message.success(res.data);
        this.init();
      } else {
        this.$message.error(res.msg);
      }
    },

    //批量删除
    batchDelete() {
      this.title = "确定删除";
      this.$confirm("确认删除该菜品, 是否继续?", "确定删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await batchDeleteDishes(this.ids);
          if (res.code === 1) {
            this.$message.success("删除成功");
            this.init();
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //批量启售、停售
    batchEdit(type) {
      this.$confirm("确认更改该菜品状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          if (type === "start") {
            // 批量启售
            const res = await startDishStatus(this.ids);
            if (res.code === 1) {
              this.$message.success(res.data);
              this.init();
            } else {
              this.$message.error(res.msg);
            }
          } else {
            //批量停售
            const res = await stopDishStatus(this.ids);
            if (res.code === 1) {
              this.$message.success(res.data);
              this.init();
            } else {
              this.$message.error(res.msg);
            }
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消更改",
          });
        });
    },

    //用户勾选发生改变
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(
        "用户勾选发生改变this.multipleSelection",
        this.multipleSelection
      );
      for (let i = 0; i < this.multipleSelection.length; i++) {
        this.ids =
          i === 0
            ? this.multipleSelection[i].id
            : this.ids + "," + this.multipleSelection[i].id;
      }
      console.log("ids", this.ids);
    },
  },
};
</script>

<style lang="scss" scoped>
.top-area {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
  margin-bottom: 15px;
  // background-color: red;

  // .el-autocomplete {
  .el-input {
    width: 310px;
    height: 60px;
    line-height: 60px;
    font-size: 16px;

    .el-input__inner {
      padding: 0 15px !important;
    }
  }

  //右侧的按钮群
  .button-group {
    .el-link {
      margin: 0 25px;
    }
    .el-button {
      width: 140px;
      height: 40px;
      font-size: 16px;
      color: #333;
      background-color: #ffc200;
    }
  }
}

.table-container {
  .el-table {
    border: solid 2px #f3f4f7;
    border-radius: 2px;
  }
  .el-pagination-container {
    display: flex;
    justify-content: center; /* 水平居中 */
    margin: 15px 0 0 0; /* 上下间距 */
  }
}

.el-form-item {
  .flavorBox {
    width: 777px;
    .flavor {
      border: solid 1px #dfe2e8 !important;
      border-radius: 3px;
      padding: 15px;
      background: #fafafb;
    }
  }
}
</style>
